/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author phihai
 */
public class Topic_DAO {
    
    Database db = new Database();
    Connection conn = null;
    PreparedStatement pst = null;
    
    public ArrayList<Topic_DTO> DanhSachTopic(int IDsite) throws Exception
    {  
        ArrayList<Topic_DTO> arr = new ArrayList<Topic_DTO>(); 
        String sql = "SELECT * FROM ChuyenMuc WHERE IDWeb="+IDsite;      
        conn = db.GetConn();
        pst = conn.prepareStatement(sql);
        
        ResultSet rs = pst.executeQuery();
        while (rs.next())
        {           
            Topic_DTO topic = new Topic_DTO();
            topic.setId(rs.getInt("IDChuyenmuc"));
            topic.setTen(rs.getString("TenChuyenMuc"));
            arr.add(topic);
        }
        
        return arr;
    }
    
    public ArrayList<Topic_DTO> lay_Xpath(int IDchuyenmuc) throws Exception
    {  
        ArrayList<Topic_DTO> arr = new ArrayList<Topic_DTO>(); 
        String sql = "SELECT * FROM ChuyenMuc WHERE IDChuyenMuc="+IDchuyenmuc;      
        conn = db.GetConn();
        pst = conn.prepareStatement(sql);
        
        ResultSet rs = pst.executeQuery();
        while (rs.next())
        {           
            Topic_DTO topic = new Topic_DTO();
            topic.setId(rs.getInt("IDChuyenmuc"));
            topic.setTen(rs.getString("TenChuyenMuc"));
            topic.set_link(rs.getString("Xpath_link")) ;
            topic.set_title(rs.getString("Xpath_title")) ;
            topic.set_img(rs.getString("Xpath_img")) ;
            topic.set_time(rs.getString("Xpath_time")) ;
            topic.set_content(rs.getString("Xpath_content")) ;
            topic.set_nextt(rs.getString("Xpath_next")) ;
            topic.setUrl(rs.getString("url")) ;
            
            arr.add(topic);
        }
        
        return arr;
    }
    
     public void themchuyenmuc(int site, String Tenchuyenmuc) throws SQLException
    {
        String sql = "insert into ChuyenMuc(IDWeb,TenChuyenMuc) values(?,?)";  
        conn = db.GetConn();
        pst = conn.prepareStatement(sql);
        pst.setInt(1, site);
        pst.setString(2, Tenchuyenmuc);
        pst.executeUpdate();
        
        
    }
     
     public void capnhat_xpath(int ID, String link, String title, String img, String time, String content, String next, String url) throws SQLException
     {
         String sql = "update ChuyenMuc set Xpath_link = ?, Xpath_title = ? , Xpath_img = ? , Xpath_time = ? , Xpath_content = ? , Xpath_next = ? , Url = ? where IDChuyenmuc = ?";
         
         conn = db.GetConn();
        pst = conn.prepareStatement(sql);
        pst.setString(1, link);
        pst.setString(2, title);
        pst.setString(3, img);
        pst.setString(4, time);
        pst.setString(5, content);
        pst.setString(6, next);
        pst.setString(7, url);
        pst.setInt(8, ID);
        
        
        pst.executeUpdate();
         
     }
}
